home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / vbasic / bv0442.exe / HUGEARR.TXT < prev    next >
Text File  |  1991-06-27  |  15KB  |  454 lines

  1.                                 HUGEARR.DLL
  2.                Huge array support for Microsoft Visual Basic
  3.  
  4.               from Microsoft Product Support Services 6/4/91
  5.  
  6. The following information applies to Microsoft Visual Basic 
  7. programming system version 1.0 for Microsoft Windows, and to Microsoft 
  8. Windows 3.0 Software Development Kit (SDK).
  9.  
  10. HUGEARR.DLL is a dynamic-link library (DLL) which contains functions 
  11. for creation, maintenance, and deletion of arrays larger than 64K from 
  12. Microsoft Visual Basic version 1.00 for Windows. This DLL also gives 
  13. the ability to create arrays with more than 32,767 (32K) elements per 
  14. dimension, and to redimension arrays while preserving the data inside 
  15. of the arrays.
  16.  
  17. The following files are provided:
  18.  
  19.    HUGEARR.DLL, HUGEARR.BAS, HUGEARR.C, HUGEARR.DEF, HUGEARR.H,  
  20.    HUGEARR.TXT, MAKEFILE  
  21.  
  22. To use the functions in HUGEARR.DLL, simply copy the declarations 
  23. contained in HUGEARR.BAS into your global module in Visual Basic and 
  24. copy HUGEARR.DLL to your Windows directory. The functions can then be 
  25. used like any other Windows DLL function.
  26.  
  27. HUGEARR.DLL allocates memory using the Windows API function 
  28. GlobalAlloc. This means that the largest array that can be allocated 
  29. is 1 MB in standard mode, and 64 MB in 386 enhanced mode for Windows.
  30.  
  31. The following routines are contained in HUGEARR.DLL. For a complete
  32. description of the parameters and/or return values of these routines,
  33. see Visual Basic's Declare statement for the routine in question in 
  34. the file HUGEARR.BAS. 
  35.  
  36. HUGEARR.DLL Language Reference:
  37. ------------------------------
  38.  
  39. -------------------------------------------------------------------------
  40. HugeDim:
  41.  
  42. Action: Dimensions an array and returns a handle to that array.
  43.  
  44. Syntax: HugeDim(recsize%, limit&)
  45.  
  46. Argument       Description
  47. --------       -----------
  48.  
  49. recsize%       The size of each element in the array.  (i.e. an integer
  50.                would be 2, a double would be 8.)  You can use the Len()
  51.                function to determine the size of any data type if you are
  52.                unsure.
  53.  
  54. limit&         The upper bound of the array.  The lower bound of all arrays
  55.                is 0, so for example: 'HugeDim(2, 10)' would create an 
  56.                integer array of elements 0 through 10.
  57.  
  58. Remarks:
  59. -------
  60.  
  61. You should not try to create a huge array of variable-length strings, or of
  62. a user-defined type that contains variable-length strings.  Visual Basic's
  63. string handling routines would not know of the existence of any string that
  64. was controlled by HUGEARR.DLL, and a UAE ("Unrecoverable Application Error")
  65. would probably result.  Fixed-length strings are okay though.
  66.  
  67. If the total size (in bytes) of the array is going to be bigger than 64K,
  68. the size of each element has to be an integer power of two (1, 2, 4, 8,
  69. and so forth.)  This is necessary so that the no element of an array 
  70. straddles a segment boundary.
  71.  
  72. HugeDim returns a handle to the array that was created, and that handle is
  73. used when referring to that array with other commands.  If an error
  74. occurred (such as out of memory), it will return a negative number.  Error
  75. codes are detailed below under the section entitled 'Error Codes'.
  76.  
  77. Example:
  78. -------
  79.  
  80. Sub Command1_Click()
  81.      Dim hArray as integer
  82.      Dim variable as SomeUserDefinedType
  83.  
  84.      hArray = HugeDim(Len(variable), 10)
  85.      If hArray < 0 Then
  86.           print "Error dimensioning array:"; hArray
  87.           Stop
  88.      End If
  89.           .
  90.           .
  91.           .
  92.      i% = HugeErase(hArray)
  93. End Sub
  94.  
  95. -------------------------------------------------------------------------
  96. HugeErase:
  97.  
  98. Action: Erases an array that was previously dimensioned using HugeDim.
  99.  
  100. Syntax: HugeErase(hArray%)
  101.  
  102. Argument       Description
  103. --------       -----------
  104.  
  105. hArray%        The handle to the array.  (The same handle that was returned
  106.                by HugeDim.)
  107.  
  108. Remarks:
  109. --------
  110.  
  111. You should be sure to HugeErase all arrays that were HugeDim'd.  Failing to
  112. do so would cause the memory used by the array to not be freed up until the
  113. application quits.  If many arrays are dimensioned but never erased memory
  114. would keep being allocated without being freed, eventually degrading system
  115. performance.
  116.  
  117. Example:  Refer to the example for HugeDim.
  118.  
  119. -------------------------------------------------------------------------
  120. HugeRedim:
  121.  
  122. Action:  Redimensions an array created with HugeDim to a different size.
  123.  
  124. Syntax:  HugeRedim(hArray%, limit&)
  125.  
  126. Argument       Description
  127. --------       -----------
  128.  
  129. hArray%        The handle to the array to redimension.
  130.  
  131. limit&         The new upper bound of the array.
  132.  
  133. Remarks:
  134. --------
  135.  
  136. HugeRedim, unlike Visual Basic's ReDim, preserves all the data in the
  137. array.  If you want to erase the contents of the array, you should erase it
  138. and the dimension it again.
  139.  
  140. If the size of the array would go over 64K when it is redimensioned, it is
  141. subject to the same size restrictions detailed in the discussion of
  142. HugeDim.
  143.  
  144. You cannot change the size of the elements in the array, only the number of
  145. elements.
  146.  
  147. Example:
  148. --------
  149.  
  150. Sub Command1_Click()
  151.      .
  152.      .
  153.      i% = HugeRedim(hArray, 50)
  154.      if i% < 0 then
  155.           print "error redimensioning array:"; hArray
  156.           stop
  157.      End If
  158.  
  159.      e% = HugeErase(hArray)
  160. End Sub
  161.  
  162. -------------------------------------------------------------------------
  163. GetHugeEl, SetHugeEl:
  164.  
  165. Action:  Gets or sets the contents of an array element.
  166.  
  167. Syntax:  GetHugeEl(hArray%, el&, variable)
  168.         SetHugeEl(hArray%, el&, variable)
  169.  
  170. Argument       Description
  171. --------       -----------
  172.  
  173. hArray%        The handle to the array.
  174.  
  175. el&            The element of the array to set or get the data from.
  176.  
  177. variable       The variable to get into, or to set the array from.
  178.  
  179. Remarks:
  180. --------
  181.  
  182. It is extremely important that the type for the variable passed to
  183. GetHugeEl or SetHugeEl matches that type of the variable used in the
  184. HugeDim statement, if the types are of different lengths, you will mostly
  185. likely get a UAE or overwrite other data.  If you want to be sure that the
  186. types match you can use the Alias keyword when you declare the function to
  187. introduce type checking.  Refer to the section below entitled 'Aliasing'
  188. for more information on aliases.
  189.  
  190. Example:
  191. --------
  192. Sub Command2_Click()
  193.      .
  194.      .
  195.      .
  196.      hArray = HugeDim(len(i%), 10)
  197.      If hArray < 0 Then Stop
  198.      e% = SetHugeEl(hArray, 1, 54%)               ' puts 54 into element #1
  199.      If e% < 0 Then Stop                ' check error code
  200.      e% = GetHugeEl(hArray, 1, i%)           ' get element #1 into i%
  201.      Print i%
  202.  
  203.      e% = HugeErase(hArray)
  204. End Sub
  205.  
  206. -----------------------------------------------------------------------
  207. HugeInt:
  208. HugeLong:
  209. HugeSingle:
  210. HugeDouble:
  211. HugeCurrency:
  212.  
  213. Action:  Retrieves an element from an array of a given type.
  214.  
  215. Syntax:  HugeInt(hArray%, el&)
  216.          HugeLong(hArray%, el&)
  217.          HugeSingle(hArray%, el&)
  218.          HugeDouble(hArray%, el&)
  219.          HugeCurrency(hArray%, el&)
  220.  
  221. Argument       Description
  222. --------       -----------
  223.  
  224. hArray%        The handle to the array.
  225.  
  226. el&            The element to retrieve.
  227.  
  228. Remarks:
  229. --------
  230.  
  231. This family of functions is provided as a shortcut alternative to GetHugeEl
  232. to retrieve a given data type while in an expression.  For example:
  233.  
  234.      value = HugeDouble(hArray, 0) * HugeDouble(hArray, 1)
  235.  
  236. The example above could have been done using GetHugeEl; however, the values
  237. returned by the two HugeDouble calls would have to be assigned to
  238. variables, and then the variables would be used in the expression.  The
  239. example below expands more on this.
  240.  
  241. IMPORTANT: None of these functions return error codes, so you should use
  242. them only if you are positive that the value of hArray% and el& are legal
  243. values.  If a error does occur (such as a "Subscript Out of Range"), you
  244. will get meaningless results.  You should use GetHugeEl if you need to be
  245. able to check error codes.
  246.  
  247. Example:
  248. --------
  249.  
  250. Sub Command3_Click()
  251.      Dim hArray As Integer
  252.  
  253.      hArray = HugeDim(len(i%), 10)
  254.      If hArray < 0 Then Stop
  255.  
  256.      e% = SetHugeEl(hArray, 0, 3%)           ' Put 3 in element #0
  257.      If e% < 0 Then Stop                     ' Check for errors
  258.      e% = SetHugeEl(hArray, 1, 4%)           ' Put 4 in element #1
  259.      If e% < 0 Then Stop
  260.  
  261.      e% = GetHugeEl(hArray, 0, i%)           ' Get value of element #0
  262.      If e% < 0 Then Stop
  263.      e% = GetHugeEl(hArray, 1, j%)           ' Get value of element #1
  264.      If e% < 0 Then Stop
  265.      Print Sqr(i% ^ 2 + j ^ 2)
  266.  
  267.                                              ' Alternate (and faster) 
  268.                                              ' way of doing the above.
  269.      Print Sqr(HugeInt(hArray, 0) ^ 2 + HugeInt(hArray, 1) ^ 2)
  270.  
  271.      e% = HugeErase(hArray)
  272. End Sub
  273.  
  274. -------------------------------------------------------------------------
  275. HugeUbound:
  276.  
  277. Action:  Returns the upper bound of a give array.
  278.  
  279. Syntax:  HugeUbound(hArray%)
  280.  
  281. Argument       Description
  282. --------       -----------
  283.  
  284. hArray%        The handle to the array.
  285.  
  286. Remarks:
  287. --------
  288.  
  289. This function is the same as Basic's 'Ubound' function.  It is used to
  290. return the current upper bound of an array as a Long.
  291.  
  292. If HugeUbound returns an negative value, it represents an error code.
  293.  
  294. Example:
  295. --------
  296.  
  297. Sub Command4_Click()
  298.      Dim hArray as integer
  299.  
  300.      hArray = HugeDim(len(i%), 23)
  301.      If hArray < 0 Then Stop
  302.  
  303.      for j=0 to HugeUbound(hArray)           ' Initialize array to 10's
  304.           e% = SetHugeEl(hArray, j, 10%)
  305.           If e% < 0 Then Stop
  306.      Next j
  307.      .
  308.      .
  309.      .
  310. End Sub
  311.  
  312. ------------------------------------------------------------------------
  313. NumHugeArrays:
  314.  
  315. Action:  Returns the number of free huge arrays available.
  316.  
  317. Syntax:  NumHugeArrays
  318.  
  319. Remarks:
  320. -------
  321.  
  322. This command is included mostly for debugging purposes.  It is used to find
  323. out how many array could be dimensioned at that time by the program.
  324.  
  325. Example:
  326. --------
  327.  
  328. Sub Command5_Click()
  329.  
  330.      Debug.Print NumHugeArrays
  331.  
  332. End Sub
  333.  
  334. ---------------------------------------------------------------------------
  335. Error Codes:
  336.  
  337. The following functions return error codes as described below:
  338.  
  339.      HugeDim, HugeErase, HugeRedim, GetHugeEl, SetHugeEl, HugeUbound
  340.  
  341. The possible error codes are:
  342.  
  343.      0    The function was successful.
  344.  
  345.      -1   "Out of Memory"  There is not enough global memory for the
  346.           HugeDim or HugeRedim
  347.  
  348.      -2   "To Many Arrays"  There are no free arrays left to be
  349.           dimensioned.
  350.  
  351.      -3   "Bad Element Size"  The array that you are trying to dimension is
  352.           greater than 64K, and the element size is not an integer power of
  353.           2.
  354.  
  355.      -4   "Subscript out of Range"  The array element you are trying to
  356.           access is outside the bounds of the array.
  357.  
  358.      -5   "Illegal Array Handle"  The array that was referenced is not a
  359.           valid array.  Either it is not dimensioned, or the handle value
  360.           is illegal.
  361. ------------------------------------------------------------------------
  362. Aliasing:
  363.  
  364. The GetHugeEl and SetHugeEl functions transfer data back and forth from an
  365. array. Because these functions must work with any data type, they are
  366. declared 'As Any' in their declarations.  This has the disadvantage that it
  367. defeats Basic's built-in type checking, and allows the posibility of
  368. passing the wrong data type.
  369.  
  370. To work around this potential problem, you can use Basic's 'Alias' keyword
  371. in the declaration.  For example, if you wanted GetHugeEl to work with the
  372. data type 'UserType', you might rename the function to 'GetUserType', and
  373. alias it to 'GetHugeEl'.
  374.  
  375. The declaration for GetHugeEl contained in "HUGEARR.BAS"  is:
  376.  
  377.      Declare Function GetHugeEl Lib "hugearr.dll" (ByVal Index%,
  378.      ByVal el&, buffer As Any) As Integer
  379.  
  380. To force Basic to do type checking on the call, you would rewrite the
  381. declaration to be:
  382.  
  383.      Declare Function GetUserType Lib "hugearr.dll" Alias "GetHugeEl"
  384.      (ByVal Index%, ByVal el&, buffer As UserType) As Integer
  385.  
  386. Note that the 'buffer As Any' has been changed to 'buffer As UserType'.
  387. Because the function no longer has the 'As Any' type Basic will be able to
  388. raise an error if you try to pass the wrong data type.
  389.  
  390. A function can be aliased any number of times, so you may want to create an
  391. alias for each data type that you are using.
  392.  
  393. -----------------------------------------------------------------------
  394. Constants:
  395.  
  396. The SetHugeEl routine is used to assign values to an array.  For example,
  397. the following statement
  398.  
  399.      i% = SetHugeEl(hArray, 1, v%)
  400.  
  401. would assign the value stored in v% to element #1 of the array hArray.
  402. However, a problem can arise when you try to use a constant in place of the
  403. 'v%' above.  For example:
  404.  
  405.      i% = SetHugeEl(hArray, 1, 15)
  406.  
  407. In this case, Visual Basic would assume that the number 15 is an integer
  408. constant and would pass a pointer to an integer to the SetHugeEl routine; 
  409. even if hArray is a (for instance) array of double-precision numbers.  
  410. To work around this potential problem, you should always use a type suffix 
  411. when passing constants to SetHugeEl.  If you wanted to assign the value 
  412. of 15 to a double precision array, you would use the statement:
  413.  
  414.      i% = SetHugeEl(hArray, 1, 15#)
  415.  
  416. --------------------------------------------------------------------------
  417.  
  418. HUGEARR.DLL Memory and Capacity:
  419.  
  420. HUGEARR.DLL allocates memory using the Windows API function GlobalAlloc.
  421. This means that the largest array that can be allocated is 1 MB in standard
  422. mode, and 64 MB in 386 enhanced mode.
  423.  
  424. If you forget to HugeErase an array that was allocated with HugeDim,
  425. Windows will automatically deallocate the memory after your application
  426. terminates.  However, HUGEARR.DLL keeps the information it needs to
  427. maintain the arrays in it's own private area.  This means that any array
  428. which is not HugeErase'd will not have it's information released, and the
  429. array will not be marked as free.  If two applications are both using the
  430. DLL, and the first application HugeDim's all of the arrays and then quits 
  431. without HugeEraseing them, the second application will not be able to 
  432. create any arrays.
  433.  
  434. --------------------------------------------------------------------------
  435.  
  436. References:
  437.  
  438. HUGEARR.DLL is written in Microsoft C, and the C source code is 
  439. provided with this application note in HUGEARR.C and HUGEARR.H.  
  440. Advanced programmers can optionally modify and rebuild HUGEARR.DLL, by 
  441. using Microsoft C Compiler version 6.00 or 6.00a and DLL libraries 
  442. from the Microsoft Windows 3.0 Software Development Kit (SDK), and by 
  443. running NMAKE.EXE with the enclosed MAKEFILE.  The MAKEFILE tells 
  444. LINK.EXE to use the enclosed linker definition file, HUGEARR.DEF.
  445.  
  446. The following references discuss how to program Windows 3.0 DLL 
  447. routines:
  448.  
  449. 1. "Programming Windows: the Microsoft Guide to Writing Applications 
  450.    for Windows 3," by Charles Petzold (published by Microsoft Press, 
  451.    1990)
  452.  
  453. 2. Microsoft Windows 3.0 Software Development Kit
  454.